package com.mx.zb.mng.ctrl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.mx.zb.mng.bo.Agent;
import com.mx.zb.mng.bo.AgentLevel;
import com.mx.zb.mng.bo.Operator;
import com.mx.zb.mng.bo.RechargeCount;
import com.mx.zb.mng.bo.Tenant;
import com.mx.zb.mng.dao.AgentLevelMapper;
import com.mx.zb.mng.dao.AgentMapper;
import com.mx.zb.mng.dao.OperatorMapper;
import com.mx.zb.mng.dao.RechargeCountMapper;
import com.mx.zb.mng.dao.TemplateMapper;
import com.mx.zb.mng.dao.TenantMapper;
import com.mx.zb.mng.utils.Constant;
import com.mx.zb.mng.utils.context.ContextThread;
import com.mx.zb.mng.utils.db2.Page;


/**
 * 
 * @ClassName: AgentCtrl
 * @Description: 代理商
 * @author 李志伟
 * @date 2017年9月22日 下午10:11:12 
 *
 */
@Controller
@RequestMapping("/p/biz")
public class AgentCtrl {
	
	@Autowired
	AgentMapper agentMapper;
	@Autowired
	TemplateMapper templateMapper;
	@Autowired
	OperatorMapper operatorMapper;
	
	@Autowired
	AgentLevelMapper agentLevelMapper;
	
	@Autowired
	RechargeCountMapper rechargeCountMapper;
	@Autowired
	TenantMapper tenantMapper;
	
	Logger log = LoggerFactory.getLogger(this.getClass());
	
	
	@RequestMapping(value= {"/agent"}, method= {RequestMethod.GET})
	public String toTenant(Model model) {
		List<AgentLevel> levelIdList= new ArrayList<AgentLevel>();		
		levelIdList  =agentLevelMapper.selectAll();
		 model.addAttribute("levelIdList",levelIdList);
		return "agent";
	}
	
	@RequestMapping(value= {"/agent/edit"}, method= {RequestMethod.GET})
	public String toEidt(Model model,@RequestParam String id) {
		Agent  parentAgent=agentMapper.selectByPrimaryKey(ContextThread.get().getAgentId());//当前创建人所在的代理商
//		String[] templates=null;
		Long count=null;
		if (null!=parentAgent.getType()&&Constant.headquarters.equals(parentAgent.getType().trim())) {
		/*	List<Template> tList=templateMapper.findAll();
			if (tList!=null) {
				templates=new String[tList.size()];
				for(int i=0;i<tList.size();i++) {
					templates[i]=tList.get(i).getName();
				}
			}*/
		}else {
/*			templates=parentAgent.getTemplateId().split(",");
*/			count=parentAgent.getLeftCount();//创建人所在代理商名称
		}
		
		List<Agent> mList= new ArrayList<Agent>();
		
		if(id!=null&&!"".equals(id)&&!"null".equals(id)) {
			Agent pojo=agentMapper.selectByPrimaryKey(id);
			Operator op=new Operator();
			op.setAgentId(id);
			Integer agentCode=pojo.getAgentCode()==null?0:pojo.getAgentCode();
			op.setTenantCode(new Long(agentCode));
			Operator operator=operatorMapper.selectByAgentId(op);
			//Operator operator=operatorMapper.selectByPrimaryKey(pojo.getCreateOper());

			model.addAttribute("entity",pojo);
			model.addAttribute("loginName",operator.getLoginName());
	/*		String[] templatesnow=pojo.getTemplateId().split(",");
			List<String> check=new ArrayList<String>();
			List<String> nocheck=new ArrayList<String>();
			for(String s: templates) {
				boolean flag=true;
				for(String s1: templatesnow) {
					if (s.trim().equals(s1.trim())) {
						check.add(s1);
						flag=false;
					}
				}
				if (flag) {
					nocheck.add(s);
				}
			}
			model.addAttribute("templatesno",nocheck);
			model.addAttribute("templates",check);*/

		}else {//添加
			Agent pojo=new Agent();
			pojo.setLeftCount(count);
			pojo.setTotalCount(count);
			model.addAttribute("entity",pojo);
			pojo.setType(Constant.general);
			mList.add(pojo);
			String type=parentAgent.getType();
			if(Constant.general.equals(type)) {
			}else {
				Agent pojo1=new Agent();
				pojo1.setType(Constant.distributor);
				mList.add(pojo1);
			}
			/*model.addAttribute("templatesno",templates);
			model.addAttribute("templates",null);
			*/
			
		}
		List<AgentLevel> levelIdList= new ArrayList<AgentLevel>();		
		levelIdList  =agentLevelMapper.selectAll();
		 model.addAttribute("levelIdList",levelIdList);
		 model.addAttribute("mList",mList);
		return "agent_edit";
	}
	
	
	@RequestMapping(value= {"/agent/detail"}, method= {RequestMethod.GET})
	public String toDetail(Model model,@RequestParam String id) {
			Agent pojo=agentMapper.selectByPrimaryKey(id);
			AgentLevel al=new AgentLevel();
			Agent parentAgent=new Agent();
			Operator operator=new Operator();
			Long shnum=0L;//商户数量
			if(pojo!=null) {
			 al	=agentLevelMapper.selectByPrimaryKey(pojo.getLevelId());
			 operator = operatorMapper.selectByPrimaryKey(pojo.getCreateOper());	
			 parentAgent = agentMapper.selectByPrimaryKey(operator.getAgentId());	
			 pojo.setParentId(pojo.getId());
			 Tenant tenant=new Tenant();
			 tenant.setAgentId(pojo.getId());
 		     shnum=tenantMapper.countTenant(tenant);
			}
			List<Agent> pages = agentMapper.queryDetailPage(pojo);
			int num=0;//代理商数量
			if(pages!=null) {
				num=pages.size();
			}
			model.addAttribute("shnum",shnum);
			model.addAttribute("num",num);
			model.addAttribute("operator",operator);
			model.addAttribute("agent",pojo);
			model.addAttribute("parentAgent",parentAgent);
			model.addAttribute("al",al);
		return "agent_detail";
	}
	
	//代理商账号充值
	@RequestMapping(value= {"/acs"})
	public String toRecharge() {
		return "recharge";
	}
	//查询代理商
	@RequestMapping(value= {"/acs/getAgent"})
	public String getAgent(@RequestParam String ap,Model  mo) {
//		Agent pojo=agentMapper.selectByPrimaryKey(ContextThread.get().getAgentId());
		String id=ContextThread.get().getAgentId();
		
				
		Agent a=new Agent();
		a.setPhone(ap);
		/*if(PhoneFormatCheckUtils.isPhoneLegal(ap)) {
			
		}else {
			a.setAgentCode(Integer.parseInt(ap));
		}*/
		if(!"dgd+Hy6G45Kab1uqE9yPLW".equals(id)) {
			a.setParentId(ContextThread.get().getAgentId());//只能查直接下级的数据
		}
		 Agent  ag= agentMapper.findAgent(a);
		 Operator op = null;
		 RechargeCount recharge=new RechargeCount();
		 AgentLevel agentLevel=new AgentLevel();
		 if(ag!=null) {
		  op = operatorMapper.selectByPrimaryKey(ContextThread.get().getOperatorId());
		   recharge= rechargeCountMapper.findByToAgent(ag.getId());
			 agentLevel=agentLevelMapper.selectByPrimaryKey(new Integer(ag.getLevelId()));

		 }
		 mo.addAttribute("agentLevel", agentLevel);
		 mo.addAttribute("recharge", recharge);
		 mo.addAttribute("agent", ag);
		 mo.addAttribute("searchAgent", ap);
		 mo.addAttribute("operator", op);
		return "recharge";
	}
	

}
